package com.mallify.admin.controller;

import com.mallify.common.model.user.UserGroup;
import com.mallify.common.page.CommonPage;
import com.mallify.common.request.PageParamRequest;
import com.mallify.common.request.UserGroupRequest;
import com.mallify.common.response.Result;
import com.mallify.service.service.UserGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户分组表 前端控制器
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/user/group")
@Api(tags = "会员 -- 分组")
public class UserGroupController {

    @Resource
    private UserGroupService userGroupService;

    /**
     * 分页显示用户分组表
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAuthority('admin:user:group:list')")
    @ApiOperation(value = "分页显示用户分组表")
    @GetMapping(value = "/list")
    public Result<CommonPage<UserGroup>> getList(@Validated PageParamRequest pageParamRequest){
        CommonPage<UserGroup> userGroupCommonPage = CommonPage.restPage(userGroupService.getList(pageParamRequest));
        return Result.success(userGroupCommonPage);
    }

    /**
     * 新增用户分组表
     * @param userGroupRequest 新增参数
     */
    @PreAuthorize("hasAuthority('admin:user:group:save')")
    @ApiOperation(value = "新增用户分组表")
    @PostMapping(value = "/save")
    public Result<String> save(@RequestBody @Validated UserGroupRequest userGroupRequest){
        if (Boolean.TRUE.equals(userGroupService.create(userGroupRequest))){
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 删除用户分组表
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:user:group:delete')")
    @ApiOperation(value = "删除用户分组表")
    @GetMapping(value = "/delete")
    public Result<String> delete(@RequestParam(value = "id") Integer id){
        if (userGroupService.removeById(id)){
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 修改用户分组表
     * @param id integer id
     * @param userGroupRequest 修改参数
     */
    @PreAuthorize("hasAuthority('admin:user:group:update')")
    @ApiOperation(value = "修改用户分组表")
    @PostMapping(value = "/update")
    public Result<String> update(@RequestParam(value = "id") Integer id, @RequestBody @Validated UserGroupRequest userGroupRequest){
        if (Boolean.TRUE.equals(userGroupService.edit(id, userGroupRequest))){
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 查询用户分组表信息
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:user:group:info')")
    @ApiOperation(value = "查询用户分组表信息")
    @GetMapping(value = "/info")
    public Result<UserGroup> info(@RequestParam(value = "id") Integer id){
        UserGroup userGroup = userGroupService.getById(id);
        return Result.success(userGroup);
    }
}